package com.nd.qa.shareplate.script.selenium;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStreamWriter;
import java.util.List;

import com.nd.qa.shareplate.model.Module;
import com.nd.qa.shareplate.model.PageElement;
import com.nd.qa.shareplate.model.Project;
import com.nd.qa.shareplate.model.ProjectType;
import com.nd.qa.shareplate.script.macaca.CreateMacacaCase;
import com.nd.qa.shareplate.script.macaca.InitMacacaScript;
import com.nd.qa.shareplate.util.FileUtil;

public class SeleniumScriptTool {

	private Module module = null;
	private Project project = null;
	private String scriptPath = null;
	private String userName = null;
	private String userPwd = null;
	private StringBuilder content = null;
	private OutputStreamWriter outputStreamWriter = null;
	InitSeleniumScript selenium = new InitSeleniumScript();
	//
	private ProjectType projectType = null;
	
	/**
	 * 
	 * @param module 模块信息
	 * @param project	项目信息
	 * @param projectDirPath	创建的测试脚本路径
	 */
	public SeleniumScriptTool(Module module,Project project,ProjectType pType, String projectDirPath) {
		this.module = module;
		this.project = project;
		this.scriptPath = projectDirPath;
		this.projectType = pType;
	}
	
	/**
	 * 写入脚本具体内容
	 * @param scriptList
	 */
	public void generateScript(List<List<PageElement>> scriptList) {
		CreateSeletiumCase cMacacaCase = new CreateSeletiumCase();
		createScript("MainActivity",module);
		try {
			for (List<PageElement> list : scriptList) {
				StringBuilder sb = new StringBuilder();
				PageElement element = list.get(0);
				String caseName = String.format("%s%s", element.getElementTypeName(), element.getElementId());
				System.out.println("-----------------------------------------");
				int size = list.size();
				for (int i = size - 1; i >= 0; i--) {
					sb.append(writeCode(list.get(i)));
					System.out.println(list.get(i).toString());
				}
				content.append(cMacacaCase.createCase(caseName, sb));
			}
			endWrite();
		} catch (Exception e) {
			endWrite();
			e.printStackTrace();
		}
	}
	
	/**
	 * 在本地创建脚本文件并初始化
	 * @param scriptName
	 */
	private void createScript(String scriptName,Module module){
	
		content = new StringBuilder();
		FileUtil fileUtil = new FileUtil();
		String path = this.project.getProjectPackage().replaceAll("\\.", "/");
		System.out.println("path:"+path);
		path = this.scriptPath;//+"src/"+path+"/test/"
		fileUtil.createDir(path);
		String fileName = "";
		if (module.getModuleOrder()!=null) {
			fileName = path + scriptName+"Test"+module.getModuleOrder();
		}else{
			fileName = path + scriptName+"Test";
		}
		File file = fileUtil.createFile(fileName, ".java");
		try {
			//String moduleName =  this.module.getModuleName();
			outputStreamWriter = new OutputStreamWriter(new FileOutputStream(file), "UTF-8");
			outputStreamWriter.write(selenium.initSeleniumScript(this.module,projectType,file.getName().split("\\.")[0],project));
		} catch (IOException e) {
			e.printStackTrace();
		}
	}
	/**
	 * 根据元素信息，写入脚本
	 * @param element
	 * @return
	 */
	private String writeCode(PageElement element) {
		
		StringBuilder sb = new StringBuilder();
		String operate = element.getElementActionName();
		
		switch (operate) {
		case "click":
			sb.append(selenium.click(element));
			break;
		case "keys":
			sb.append(selenium.keys(element));
			break;
		case "focusClick":
			sb.append(selenium.focusClick(element));
			break;
		case "clickPoint":
			sb.append(selenium.clickPoint(element));
			break;
		case "input":
			sb.append(selenium.sendKey(element));
			break;
		case "select":
			sb.append(selenium.selectDropdownList(element));
			break;
		default:
			break;
		}
		return sb.toString();
	}
	
	private void endWrite(){
		if (outputStreamWriter != null) {
			try {
				outputStreamWriter.write(content.toString());
				outputStreamWriter.write("}");
				outputStreamWriter.close();
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
	}
}
